﻿using AzureContrib.WindowsAzure.StorageClient.Tests.Mocks;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureContrib.WindowsAzure.StorageClient.IntegrationTests
{
	[TestClass]
	public class DevelopmentStorageIntegrationTests
	{
		bool useFiddler = false;
		ICloudStorageAccountManager cloudStorageAccountManager;

		[TestInitialize]
		public void Create_CloudStorageAccountManager()
		{
			cloudStorageAccountManager = new DevelopmentStorageCloudStorageAccountManager(useFiddler);
		}

		[TestMethod]
		public void BlobExists()
		{
			var storageAccount = new DevelopmentStorageCloudStorageAccountManager(useFiddler).GetStorageAccount("what ever");

			string containerUri = storageAccount.BlobEndpoint.AbsoluteUri + "/foocontainer";
			string blobUri = containerUri + "/foo.bar.baz";

			var cloudBlobContainer = new CloudBlobContainer(containerUri, storageAccount.Credentials.Unwrap());
			cloudBlobContainer.CreateIfNotExist();

			var cloudBlob = new CloudBlob(blobUri, storageAccount.Credentials.Unwrap());

			cloudBlob.DeleteIfExists();

			Assert.IsFalse(cloudBlob.Exists());

			cloudBlob.UploadText("some text");

			Assert.IsTrue(cloudBlob.Exists());
			Assert.IsTrue(cloudBlob.Exists());

			cloudBlobContainer.Delete();
		}

		[TestMethod]
		public void Create_table_through_abstractions()
		{
			ICloudTableClient cloudTableClient = cloudStorageAccountManager.GetCloudTableClient("something does not matter");

			string tableName = "dude";

			cloudTableClient.CreateTableIfNotExist(tableName);

			Assert.IsTrue(cloudTableClient.DoesTableExist(tableName));

			cloudTableClient.DeleteTable(tableName);

			Assert.IsFalse(cloudTableClient.DoesTableExist(tableName));
		}

		[TestMethod]
		public void Create_table_no_abstractions()
		{
			var cloudStorageAccount = cloudStorageAccountManager.GetStorageAccount("something does not matter");
			CloudTableClient cloudTableClient = new CloudTableClient(cloudStorageAccount.TableEndpoint.AbsoluteUri, cloudStorageAccount.Credentials.Unwrap());

			string tableName = "dude";

			cloudTableClient.CreateTableIfNotExist(tableName);

			Assert.IsTrue(cloudTableClient.DoesTableExist(tableName));

			cloudTableClient.DeleteTable(tableName);

			Assert.IsFalse(cloudTableClient.DoesTableExist(tableName));
		}
	}
}